package com.lg.erp.controller.zhangchaofu;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.zhangchaofu.*;
import com.lg.erp.service.zhangchaofu.ICTCargoAreaService;
import com.lg.erp.service.zhangchaofu.ICTCargoService;
import com.lg.erp.service.zhangchaofu.ICTShelvesService;
import com.lg.erp.service.zhangchaofu.ICTWareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@RestController
public class CTCargoController {

    @Autowired
    private ICTCargoService cargoService;
    @Autowired
    private ICTCargoAreaService cargoAreaService;
    @Autowired
    private ICTWareService wareService;
    @Autowired
    private ICTShelvesService shelvesService;

    @GetMapping("/cargoes")
    public JsonResult getCargoes(@RequestParam(value = "current",required = false,defaultValue="1") Long current,
                                 @RequestParam(value = "pageSize",required = false,defaultValue="10") Long pageSize){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User){
            User user=(User) principal;
            Page<CTCargo> page = new Page<>(current,pageSize);
            IPage<CTCargo> iPage = cargoService.findCargoPage(page);
            return new JsonResult(1,"ok",iPage);
        }
        else{
            return new JsonResult(0,"no",null);
        }
    }

    @PutMapping("/cargoes")
    public JsonResult modifyCargoStatus(@RequestParam("id") String id, @RequestParam("status") String status){
        cargoService.modifyCargoStatus(id,status);
        return new JsonResult(1,"ok",status+"了");
    }

    @GetMapping("/cargoSearch")
    public JsonResult getCargoSearch(String search){
        System.out.println(search);
        List<CTCargo> tCargoes = cargoService.searchCargo(search);
        return new JsonResult(1,"ok",tCargoes);
    }



    @PostMapping("/cargoes")
    public JsonResult addCargo(GoodsLocation goodsLocation){
        System.out.println(goodsLocation.toString());
        CTCargo tCargo = new CTCargo();
        List<CTCargo> cargoes = cargoService.list(
                new QueryWrapper<CTCargo>().lambda()
                        .eq(CTCargo::getCargoNo,goodsLocation.getCargoNo())
        );
        if (cargoes.isEmpty()){
            System.out.println("没有这个货位，可以添加");
            tCargo.setId(goodsLocation.getId());
            tCargo.setCargoNo(goodsLocation.getCargoNo());
            tCargo.setShelvesId(goodsLocation.getAddShelves());
            tCargo.setDamId(goodsLocation.getAddCargoArea());
            tCargo.setWareId(goodsLocation.getAddWarehouse());
            tCargo.setRemark(goodsLocation.getRemark());

            tCargo.setCreatTime(Timestamp.valueOf(LocalDateTime.now()));
            tCargo.setStatus("无货");
            boolean b = cargoService.save(tCargo);
            return new JsonResult(1,"ok","添加成功");
        }else{
            System.out.println("这个货位存在，不可再次添加");
            return new JsonResult(0,"no","这个货位存在，不可再次添加");
        }
    }

    @PutMapping("/cargoes2")
    public JsonResult modifyCargoes(GoodsLocation goodsLocation){
        //System.out.println(goodsLocation.toString());

        CTWare ware1 = wareService.getOne(new QueryWrapper<CTWare>().lambda()
                .eq(CTWare::getWareName, goodsLocation.getAddWarehouse()));
        if (ware1 !=null){
            CTCargoArea cargoArea1 = cargoAreaService.getOne(new QueryWrapper<CTCargoArea>().lambda()
                    .eq(CTCargoArea::getGoodsName, goodsLocation.getAddCargoArea())
                    .eq(CTCargoArea::getGoodsNo,goodsLocation.getGoodsNo())
                    .eq(CTCargoArea::getWareId,ware1.getId()));
            if (cargoArea1 != null){
                CTShelves shelves1 = shelvesService.getOne(new QueryWrapper<CTShelves>().lambda()
                        .eq(CTShelves::getShelvesNo, goodsLocation.getAddShelves())
                        .eq(CTShelves::getWareId,ware1.getId())
                        .eq(CTShelves::getDamId,cargoArea1.getId()));
                if (shelves1 != null){
                    CTCargo tCargo = new CTCargo();
                    tCargo.setId(goodsLocation.getId());
                    tCargo.setCargoNo(goodsLocation.getCargoNo());
                    tCargo.setShelvesId(shelves1.getId());
                    tCargo.setDamId(cargoArea1.getId());
                    tCargo.setWareId(ware1.getId());
                    tCargo.setProduct(goodsLocation.getProduct());
                    tCargo.setStatus(goodsLocation.getStatus());
                    tCargo.setRemark(goodsLocation.getRemark());
                    System.out.println(tCargo.toString());
                    int i = cargoService.modifyCargoes(tCargo);
                    System.out.println(i);
                    return new JsonResult(1,"ok","修改成功");
                }
            }
        }

        return new JsonResult(0,"no","修改失败");

    }

    @GetMapping("/cargoInline")
    public JsonResult getInline(String shelvesNo, String cargoNo, String status, String current, String pageSize){
        System.out.println(current);
        System.out.println(pageSize);
        List<CTCargo> tCargoes =cargoService.findInline(shelvesNo,cargoNo,status);
        return new JsonResult(1,"ok",tCargoes);
    }
    @GetMapping("/product/cargo/{productNo}")
    public JsonResult getProductSumByProductNO(@PathVariable("productNo") String productNo){
        Integer sum=cargoService.findProductSumByProductNO(productNo);
        return new JsonResult(1,"ok",sum);
    }
}
